package control;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

@WebServlet("/deleteOrderServlet")
public class deleteOrderServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String order_id = (String)request.getParameter("order_id");
        try{
            Connection con=null;
            Statement stmt=null;
            Class.forName("org.h2.Driver");
            /*url后面加的?useUnicode=true&characterEncoding=gbk，是为了处理向数据库中添加数据时出现乱码的问题。*/
            String url="jdbc:h2:~/test1";
            con= DriverManager.getConnection(url,"root","root");
            HttpSession session=request.getSession();
            stmt=con.createStatement();
            System.out.println(order_id);
            String sql="DELETE FROM `order` WHERE order_id = "+order_id+";";
            stmt.executeUpdate(sql);
            System.out.println("删除成功！");
            request.getSession().removeAttribute("orderlist");
            stmt.close();
            con.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        response.sendRedirect("http://localhost:8080/order_manageServlet");
        return;
    }
}
